﻿namespace Digitalmes.Application.Sys.Roles.Queries;

/// <summary>
/// 分页查询角色命令。
/// </summary>
public record GetRoleWithPagedQuery : PagedQuery, IRequest<IResult<PagedList<SysRoleDto>>>
{
    public string? Name { get; init; }

    public string? Code { get; init; }
}

internal sealed class GetRoleWithPagedQueryHandler(IRepository<SysRole> repo) : IRequestHandler<GetRoleWithPagedQuery, IResult<PagedList<SysRoleDto>>>
{
    public async Task<IResult<PagedList<SysRoleDto>>> Handle(GetRoleWithPagedQuery request, CancellationToken cancellationToken)
    {
        var roles = await repo.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(request.Name), m => m.Name.Contains(request.Name!))
            .WhereIF(!string.IsNullOrEmpty(request.Code), m => m.Code == request.Code)
            .ToPagationAsync(request.PageNumber, request.PageSize, cancellationToken);
        return Result.From(roles.Adapt<PagedList<SysRoleDto>>());
    }
}
